home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog7.arj / MDIBITS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  5.7 KB  |  210 lines

  1. { mdibits.pas -- MDI bitmap viewer }
  2.  
  3. program MDIBits;
  4.  
  5. {$R mdibits.res}
  6.  
  7. uses WinTypes, WinProcs, WObjects, WinDos, Strings, StdDlgs, UBitmap;
  8.  
  9. const
  10.  
  11.   id_Menu = 'MDIMenu';    { Menu resource ID }
  12.   posWindowMenu = 1;      { Position of Window menu in menu bar }
  13.  
  14. type
  15.  
  16.   MDIBitsApplication = object(TApplication)
  17.     procedure InitMainWindow; virtual;
  18.   end;
  19.  
  20.   PMDIBitsWindow = ^TMDIBitsWindow;
  21.   TMDIBitsWindow = object(TMDIWindow)
  22.     constructor Init(ATitle: PChar; AMenu: HMenu);
  23.     procedure MDIFileOpen(var Msg: TMessage);
  24.       virtual cm_First + cm_MDIFileOpen;
  25.   end;
  26.  
  27.   PBitmapChild = ^TBitmapChild;
  28.   TBitmapChild = object(TWindow)
  29.     Bitmap: HBitmap;          { Handle to bitmap in memory }
  30.     Width, Height: LongInt;   { Size of bitmap image }
  31.     IconBitmap: HBitmap;      { Handle to iconicized bitmap }
  32.     IWidth, IHeight: LongInt; { Size of iconicized bitmap }
  33.     constructor Init(AParent: PWindowsObject; ATitle: PChar;
  34.       Handle: HBitmap; W, H: LongInt);
  35.     destructor Done; virtual;
  36.     function GetClassName: PChar; virtual;
  37.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  38.     procedure AdjustScroller;
  39.     procedure MakeIconBitmap(DC: HDC);
  40.     procedure Paint(PaintDC: HDC;
  41.       var PaintInfo: TPaintStruct); virtual;
  42.     procedure WMSize(var Msg: TMessage);
  43.       virtual wm_First + wm_Size;
  44.   end;
  45.  
  46.  
  47. { TMDIBitsWindow }
  48.  
  49. {- Construct frame window }
  50. constructor TMDIBitsWindow.Init(ATitle: PChar; AMenu: HMenu);
  51. begin
  52.   TMDIWindow.Init(ATitle, AMenu);
  53.   ChildMenuPos := posWindowMenu
  54. end;
  55.  
  56. {- Respond to File:Open command. Create new child window. }
  57. procedure TMDIBitsWindow.MDIFileOpen(var Msg: TMessage);
  58. var
  59.   FileName: array[0 .. fsPathName] of Char;
  60.   Bitmap: HBitmap;         { Handle to bitmap }
  61.   Width, Height: LongInt;  { Bitmap's width and height in pixels }
  62. begin
  63.   StrCopy(FileName, '*.bmp');
  64.   if Application^.ExecDialog(New(PFileDialog, Init(@Self,
  65.     PChar(sd_FileOpen), FileName))) = id_Ok then
  66.   begin
  67.     SetCursor(LoadCursor(0, idc_Wait));
  68.     Bitmap := LoadBitmap(FileName, HWindow, Width, Height);
  69.     SetCursor(LoadCursor(0, idc_Arrow));
  70.     if Bitmap = 0 then
  71.       MessageBox(HWindow, 'File is not a bitmap', 'Error',
  72.         mb_IconExclamation or mb_ok)
  73.     else
  74.       Application^.MakeWindow(New(PBitmapChild,
  75.         Init(@Self, FileName, Bitmap, Width, Height)))
  76.   end
  77. end;
  78.  
  79.  
  80. { TBitmapChild }
  81.  
  82. {- Construct child window }
  83. constructor TBitmapChild.Init(AParent: PWindowsObject; ATitle: PChar;
  84.   Handle: HBitmap; W, H: LongInt);
  85. begin
  86.   TWindow.Init(AParent, ATitle);
  87.   Attr.Style := Attr.Style or ws_VScroll or ws_HScroll;
  88.   Bitmap := Handle;
  89.   IconBitmap := 0;   { Created on first use }
  90.   Width := W;
  91.   Height := H;
  92.   Scroller := New(PScroller, Init(@Self, 1, 1, 200, 200))
  93. end;
  94.  
  95. {- Destroy child window }
  96. destructor TBitmapChild.Done;
  97. begin
  98.   if Bitmap <> 0 then DeleteObject(Bitmap);
  99.   TWindow.Done
  100. end;
  101.  
  102. {- Modify child's window class to paint icon windows. }
  103. procedure TBitmapChild.GetWindowClass(var AWndClass: TWndClass);
  104. begin
  105.   TWindow.GetWindowClass(AWndClass);
  106.   AWndClass.HIcon := 0
  107. end;
  108.  
  109. {- Give the new window class a name }
  110. function TBitmapChild.GetClassName: PChar;
  111. begin
  112.   GetClassName := 'TBitmapClass'
  113. end;
  114.  
  115. {- Keep scroll bars in synch with bitmap and window sizes }
  116. procedure TBitmapChild.AdjustScroller;
  117. var
  118.   ClientRect: TRect;
  119. begin
  120.   GetClientRect(HWindow, ClientRect);
  121.   with ClientRect do
  122.     Scroller^.SetRange(Width - (Right - Left),
  123.       Height - (Bottom - Top));
  124.   InvalidateRect(HWindow, nil, true)
  125. end;
  126.  
  127. {- Respond to changes in window size }
  128. procedure TBitmapChild.WMSize(var Msg: TMessage);
  129. begin
  130.   TWindow.WMSize(Msg);
  131.   if not (Msg.WParam = sizeIconic) then
  132.     AdjustScroller
  133. end;
  134.  
  135. {- Create a small bitmap for the window's icon }
  136. procedure TBitmapChild.MakeIconBitmap(DC: HDC);
  137. var
  138.   MemDC1, MemDC2: HDC;
  139.   OldBitmap1, OldBitmap2: HBitmap;
  140.   R: TRect;
  141. begin
  142.   MemDC1 := CreateCompatibleDC(DC);
  143.   MemDC2 := CreateCompatibleDC(DC);
  144.   GetClientRect(HWindow, R);
  145.   IWidth := R.Right;
  146.   IHeight := R.Bottom;
  147.   IconBitmap := CreateCompatibleBitmap(DC, IWidth, IHeight);
  148.   OldBitmap1 := SelectObject(MemDC1, IconBitmap);
  149.   OldBitmap2 := SelectObject(MemDC2, Bitmap);
  150.   StretchBlt(MemDC1, 0, 0, IWidth, IHeight,
  151.     MemDC2, 0, 0, Width, Height, SRCCopy);
  152.   SelectObject(MemDC1, OldBitmap1);
  153.   SelectObject(MemDC2, OldBitmap2);
  154.   DeleteDC(MemDC1);
  155.   DeleteDC(MemDC2)
  156. end;
  157.  
  158. {- Paint bitmap inside window }
  159. procedure TBitmapChild.Paint(PaintDC: HDC;
  160.   var PaintInfo: TPaintStruct);
  161. var
  162.   MemDC: HDC;
  163.   Image, OldBitmap: HBitmap;
  164.   W, H: LongInt;
  165. begin
  166.   TWindow.Paint(PaintDC, PaintInfo);
  167.   if IsIconic(HWindow) then
  168.   begin
  169.     if IconBitmap = 0 then MakeIconBitmap(PaintDC);
  170.     Image := IconBitmap; W := IWidth; H := IHeight
  171.   end else
  172.   begin
  173.     Image := Bitmap; W := Width; H := Height
  174.   end;
  175.   if Image <> 0 then
  176.   begin
  177.     MemDC := CreateCompatibleDC(PaintDC);
  178.     OldBitmap := SelectObject(MemDC, Image);
  179.     BitBlt(PaintDC, 0, 0, W, H, MemDC, 0, 0, SRCCopy);
  180.     SelectObject(MemDC, OldBitmap);
  181.     DeleteDC(MemDC)
  182.   end
  183. end;
  184.  
  185.  
  186. { MDIBitsApplication }
  187.  
  188. {- Initialize MDIBitsApplication object's window }
  189. procedure MDIBitsApplication.InitMainWindow;
  190. begin
  191.   MainWindow := New(PMDIBitsWindow, Init('MDI Bitmap Viewer',
  192.     LoadMenu(HInstance, id_Menu)))
  193. end;
  194.  
  195. var
  196.  
  197.   MDIBitsApp: MDIBitsApplication;
  198.  
  199. begin
  200.   MDIBitsApp.Init('MDIBitsApp');
  201.   MDIBitsApp.Run;
  202.   MDIBitsApp.Done
  203. end.
  204.  
  205.  
  206. {--------------------------------------------------------------
  207.   Copyright (c) 1991 by Tom Swan. All rights reserved.
  208.   Revision 1.00    Date: 5/31/1991
  209. ---------------------------------------------------------------}
  210.